package com.elysia.visionmanagerspringboot.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elysia.visionmanagerspringboot.domain.dto.StudioContactDTO;
import com.elysia.visionmanagerspringboot.domain.entity.StudioContact;
import com.elysia.visionmanagerspringboot.mapper.StudioContactMapper;
import com.elysia.visionmanagerspringboot.result.Result;
import com.elysia.visionmanagerspringboot.service.IStudioContactService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
@Slf4j
public class IStudioContactServiceImpl extends ServiceImpl<StudioContactMapper, StudioContact> implements IStudioContactService {
    @Override
    public Result<Object> update(StudioContactDTO studioContactDTO) {
        lambdaUpdate().eq(StudioContact::getId, 1)
                .set(StudioContact::getQq, studioContactDTO.getQq())
                .set(StudioContact::getWechat, studioContactDTO.getWechat())
                .set(StudioContact::getEmail, studioContactDTO.getEmail()).update();
        return Result.success(lambdaQuery().eq(StudioContact::getId, 1).one());
    }

    @Override
    public Result<Object> get() {
        // 尝试从数据库中获取数据
        StudioContact studioContact = lambdaQuery().eq(StudioContact::getId, 1).one();
        // 如果数据库中没有数据，则插入默认值
        if (studioContact == null) {
            studioContact = StudioContact.builder()
                    .qq("default_qq")
                    .wechat("default_wechat")
                    .email("default_email")
                    .build();
            save(studioContact);
        }
        // 再次尝试获取数据
        studioContact = lambdaQuery().eq(StudioContact::getId, 1).one();
        return Result.success(studioContact);
    }
}
